package com.rj1101.hscrm.demo.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rj1101.hscrm.demo.model.ReVisit;

/**
 * A data access object (DAO) providing persistence and search support for
 * ReVisit entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rj1101.hscrm.demo.model.ReVisit
 * @author MyEclipse Persistence Tools
 */

public class ReVisitDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(ReVisitDAO.class);
	// property constants
	public static final String VISIT_PLAN_ID = "visitPlanId";
	public static final String VISITOR = "visitor";
	public static final String VISIT_TYPE = "visitType";
	public static final String VISIT_DATE = "visitDate";
	public static final String APPOINT_ID = "appointId";
	public static final String VISIT_STATUS = "visitStatus";
	public static final String VISIT_CONTENT = "visitContent";

	protected void initDao() {
		// do nothing
	}

	public void save(ReVisit transientInstance) {
		log.debug("saving ReVisit instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ReVisit persistentInstance) {
		log.debug("deleting ReVisit instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ReVisit findById(java.lang.Integer id) {
		log.debug("getting ReVisit instance with id: " + id);
		try {
			ReVisit instance = (ReVisit) getHibernateTemplate().get(
					"com.rj1101.hscrm.demo.model.ReVisit", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ReVisit instance) {
		log.debug("finding ReVisit instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ReVisit instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ReVisit as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByVisitPlanId(Object visitPlanId) {
		return findByProperty(VISIT_PLAN_ID, visitPlanId);
	}

	public List findByVisitor(Object visitor) {
		return findByProperty(VISITOR, visitor);
	}

	public List findByVisitType(Object visitType) {
		return findByProperty(VISIT_TYPE, visitType);
	}

	public List findByVisitDate(Object visitDate) {
		return findByProperty(VISIT_DATE, visitDate);
	}

	public List findByAppointId(Object appointId) {
		return findByProperty(APPOINT_ID, appointId);
	}

	public List findByVisitStatus(Object visitStatus) {
		return findByProperty(VISIT_STATUS, visitStatus);
	}

	public List findByVisitContent(Object visitContent) {
		return findByProperty(VISIT_CONTENT, visitContent);
	}

	public List findAll() {
		log.debug("finding all ReVisit instances");
		try {
			String queryString = "from ReVisit";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ReVisit merge(ReVisit detachedInstance) {
		log.debug("merging ReVisit instance");
		try {
			ReVisit result = (ReVisit) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ReVisit instance) {
		log.debug("attaching dirty ReVisit instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ReVisit instance) {
		log.debug("attaching clean ReVisit instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ReVisitDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ReVisitDAO) ctx.getBean("ReVisitDAO");
	}
}